﻿using System;

class MatrixSequences
{
    static void Main()
    {
        string[,] array = {
            {"ha", "bg", "ha", "sa", "sa"},
            {"oh", "ha", "ha", "sa", "az"},
            {"ha", "pa", "sa", "az", "bg"},
            {"oh", "bg", "az", "ha", "fr"},
            {"sa", "az", "bg", "be", "be"}
        };

        // Printing the array on the console
        for (int row = 0; row < array.GetLength(0); row++)
        {
            for (int col = 0; col < array.GetLength(1); col++)
            {
                Console.Write("{0,3}", array[row, col]);
            }

            Console.WriteLine();
        }

        int longestLine = 1;
        string longestString = array[0, 0];

        for (int row = 0; row < array.GetLength(0); row++)
        {
            for (int col = 0; col < array.GetLength(1); col++)
            {
                OppositeDiagonalSearch(array, ref longestLine, ref longestString, row, col);

                DownwardsSearch(array, ref longestLine, ref longestString, row, col);

                DiagonalSearch(array, ref longestLine, ref longestString, row, col);

                RightDirectionsSearch(array, ref longestLine, ref longestString, row, col);
            }
        }

        Console.WriteLine("The longest sequence is:");
        Console.WriteLine("string: {0}, length: {1}", longestString, longestLine);
    }

    private static void OppositeDiagonalSearch(string[,] array, ref int longestLine, ref string longestString, int row, int col)
    {
        int tempLine = 1;
        string tempString = array[row, col];

        for (; (col < array.GetLength(1) - 1) && row > 0; col++, row--)
        {
            if (array[row, col] == array[row - 1, col + 1])
            {
                tempLine++;
            }
            else
            {
                if (tempLine > longestLine)
                {
                    longestLine = tempLine;
                    longestString = tempString;
                }

                tempLine = 1;
                tempString = array[row - 1, col + 1];
            }
        }

        // Special last case
        if (tempLine > longestLine)
        {
            longestLine = tempLine;
            longestString = tempString;
        }
    }

    private static void DiagonalSearch(string[,] array, ref int longestLine, ref string longestString, int row, int col)
    {
        int tempLine = 1;
        string tempString = array[row, col];

        for (; (col < array.GetLength(1) - 1) && row < (array.GetLength(1) - 1); col++, row++)
        {
            if (array[row, col] == array[row + 1, col + 1])
            {
                tempLine++;
            }
            else
            {
                if (tempLine > longestLine)
                {
                    longestLine = tempLine;
                    longestString = tempString;
                }

                tempLine = 1;
                tempString = array[row + 1, col + 1];
            }
        }

        // Special last case
        if (tempLine > longestLine)
        {
            longestLine = tempLine;
            longestString = tempString;
        }
    }

    private static void RightDirectionsSearch(string[,] array, ref int longestLine, ref string longestString, int row, int col)
    {
        int tempLine = 1;
        string tempString = array[row, col];

        for (; col < array.GetLength(1) - 1; col++)
        {
            if (array[row, col] == array[row, col + 1])
            {
                tempLine++;
            }
            else
            {
                if (tempLine > longestLine)
                {
                    longestLine = tempLine;
                    longestString = tempString;
                }

                tempLine = 1;
                tempString = array[row, col + 1];
            }
        }

        // Special last case
        if (tempLine > longestLine)
        {
            longestLine = tempLine;
            longestString = tempString;
        }
    }

    private static void DownwardsSearch(string[,] array, ref int longestLine, ref string longestString, int row, int col)
    {
        int tempLine = 1;
        string tempString = array[row, col];

        for (; row < array.GetLength(0) - 1; row++)
        {
            if (array[row, col] == array[row + 1, col])
            {
                tempLine++;
            }
            else
            {
                if (tempLine > longestLine)
                {
                    longestLine = tempLine;
                    longestString = tempString;
                }

                tempLine = 1;
                tempString = array[row + 1, col];
            }
        }

        // Special last case
        if (tempLine > longestLine)
        {
            longestLine = tempLine;
            longestString = tempString;
        }
    }
}
